package com.my.demo.demo2.config;

import com.retrofit.starter.okhttp.OkHttpClientConfiguration;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author shanchong
 * @date 2019-07-12
 **/
@Configuration
@EnableConfigurationProperties(RetrofitProperty.class)
public class RetrofitConfiguration {

    @Bean
    public OkHttpClientConfiguration getOkHttpClientConfig(RetrofitProperty retrofitProperty){
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.proxySelector(new MultiProxySelector(retrofitProperty.getRetrys()))
                .connectionPool(new ConnectionPool(retrofitProperty.getMaxIdleConnections(),retrofitProperty.getKeepAliveTimeout(), TimeUnit.SECONDS))
                .connectTimeout(retrofitProperty.getConnectionTimeout(),TimeUnit.SECONDS)
                .readTimeout(retrofitProperty.getReadTimeout(),TimeUnit.SECONDS)
                .writeTimeout(retrofitProperty.getWriteTimeout(),TimeUnit.SECONDS);
        return new OkHttpClientConfiguration(builder.build());
    }
}
